<?php

/**
 * Created by JetBrains PhpStorm.
 * User: GMC
 * Date: 10/3/13
 * Time: 2:20 PM
 * To change this template use File | Settings | File Templates.
 */
class WidgetShopTypical extends CWidget {
    public $shops;

    public function init() {
        $shop_typical = Yii::app()->cache->get('shop_typical');
//        if ($shop_typical == null || count($shop_typical) == 0) {
        $shops = Ads::model()->findAll(array(
            'select' => 'ads_id, ads_name, description, url, image_path',
            'condition' => 'position = :position',
            'params' => array(':position' => 'shop-tieubieu')
        ));


        $models = array();
        $result = array();
        $num = 1;
        $index = 0;
        foreach ($shops as $key => $item) {
            array_push($result, array(
                'id' => $item->ads_id,
                'name' => $item->ads_name,
                'picture' => $item->image_path,
                'description' => $item->description,
                'url' => $item->url,
            ));
            if ($num == 3 || $key == count($shops) - 1) {
                $models[$index] = $result;
                $result = array();
                $index++;
                $num = 1;
            } else {
                $num++;
            }
        }

        Yii::app()->cache->set('shop_typical', $models, 'shop_typical_time');
        $shop_typical = Yii::app()->cache->get('shop_typical');
//        }
        $this->shops = $shop_typical;
    }

    public function run() {
        return CWidget::render('widgetShopTypical', array('model' => $this->shops));
    }
}